package org.jb.coinche.server.user.room.impl;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.jb.coinche.client.user.UserLogin;
import org.jb.coinche.server.user.room.RoomUsersNotifier;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

@Singleton
public class RoomUsersFilter implements Filter {

	@Inject
	private Provider<RoomUsersNotifier> roomUsersNotifier;
	
	@Inject @UserLogin
	private Provider<String> user;
	
	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		String userName = user.get();
		
		if (userName != null) {
			roomUsersNotifier.get().notifyActivity(userName);
		}
		
		chain.doFilter(request, response);

	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {}

}
